package com.twxm.breed.controller;

import com.alibaba.fastjson.JSONObject;
import com.twxm.breed.entity.AccessToken;
import com.twxm.breed.entity.PushMessageEntity;
import com.twxm.breed.entity.ResultTemplateDate;
import com.twxm.breed.util.JsonResult;
import com.twxm.breed.util.ResultCode;
import com.twxm.breed.util.SHA1Util;
import com.twxm.breed.util.WebChatUtil;
import lombok.extern.slf4j.Slf4j;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

@RestController
@RequestMapping(value = "/wx")
@Slf4j
public class WechatMsgController {

    /**
     * 验证微信消息
     *
     * @param request
     * @return
     */
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping(value = "/check")
    public String checkWxMsg(HttpServletRequest request) throws NoSuchAlgorithmException {
        log.info("进入微信校验的接口了");
        /**
         * 微信加密签名
         */
        String signature = request.getParameter("signature");
        /**
         * 随机字符串
         */
        String echostr = request.getParameter("echostr");
        /**
         * 时间戳
         */
        String timestamp = request.getParameter("timestamp");
        /**
         * 随机数
         */
        String nonce = request.getParameter("nonce");

        String[] str={timestamp,nonce, "breedtwxm"};
        //将token、timestamp、nonce三个参数进行字典序排序
        Arrays.sort(str);
        StringBuffer sb = new StringBuffer();
        //将三个参数字符串拼接成一个字符串进行sha1加密
        for (String param:str) {
            sb.append(param);
        }

        //获取到加密过后的字符串
        String encryptStr = SHA1Util.encrypt(sb.toString());
        //判断加密后的字符串是否与微信的签名一致
        if(signature.equalsIgnoreCase(encryptStr)){
            log.info("签名一致可以通过");
            return echostr;
        }

        log.error("这不是微信发来的消息！！");
        return null;
    }


    @RequestMapping(value = "/push/message", method = { RequestMethod.POST })
    public JsonResult pushMessage(@RequestBody PushMessageEntity pushMessageEntity)
            throws ClientProtocolException, IOException {
        System.out.println("Into pushMessage method");
        // Get AccessToken
        String accessToken = getAccessToken();
        if (accessToken.equals("")) {
            return new JsonResult(ResultCode.FAIL, "无法通过APPID和APPSecret获取AccessToken");
        }
        System.out.println("AccessToken = " + accessToken);
        // WeiXin Push Message URL
        String pushMessageUrl = WebChatUtil.SEND_TEMPLAYE_MESSAGE_URL + getAccessToken();
        // Get Mobile Array List
        String[] openidlist = pushMessageEntity.getOpenidlist();

        ArrayList<String> sendSuccessUsers = new ArrayList<String>();
        ArrayList<String> sendFailUsers = new ArrayList<String>();

        JSONObject data = pushMessageEntity.getData();
        String template_id = pushMessageEntity.getTemplate_id();
        if (null == template_id || template_id.equals("")) {
            return new JsonResult(ResultCode.DATANOTFOUND, "无法从PushMessageEntity获取template_id");
        }
        String url = pushMessageEntity.getUrl();
        if (null == url || url.equals("")) {
            return new JsonResult(ResultCode.DATANOTFOUND, "无法从PushMessageEntity获取url");
        }
        String params = generatePushTemplate(data, template_id,url);
        if (params.equals("")) {
            return new JsonResult(ResultCode.EXCEPTION, "generatePushTemplate()失败");
        }

        HttpPost httpPost = new HttpPost(pushMessageUrl);
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 装配post请求参数
        for (String openid : openidlist) {
            StringBuffer buffer = new StringBuffer();
            // 按照官方api的要求提供params
            buffer.append("{");
            buffer.append(String.format("\"touser\":\"%s\"", openid)).append(",");
            buffer.append(params);
            String urlParams = new String(buffer.toString().getBytes("UTF-8"));


            StringEntity myEntity = new StringEntity(urlParams, ContentType.APPLICATION_JSON);
            System.out.println("Push Message urlParams = " + urlParams);

            httpPost.setEntity(myEntity);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            String resultStr = "发送失败";
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                // 发送成功
                String resutlEntity = EntityUtils.toString(httpResponse.getEntity());
                ResultTemplateDate resultTemplateDate = JSONObject.parseObject(resutlEntity, ResultTemplateDate.class);
                if (resultTemplateDate.getErrcode().equals("40037")) {
                    resultStr = "template_id不正确";
                    sendFailUsers.add(openid);
                }
                if (resultTemplateDate.getErrcode().equals("41028")) {
                    resultStr = "form_id不正确，或者过期";
                    sendFailUsers.add(openid);
                }
                if (resultTemplateDate.getErrcode().equals("41029")) {
                    resultStr = "form_id已被使用";
                    sendFailUsers.add(openid);
                }
                if (resultTemplateDate.getErrcode().equals("41030")) {
                    resultStr = "page不正确";
                    sendFailUsers.add(openid);
                }
                if (resultTemplateDate.getErrcode().equals("45009")) {
                    resultStr = "接口调用超过限额（目前默认每个帐号日调用限额为100万）";
                    sendFailUsers.add(openid);
                }
                resultStr = "ok";
                sendSuccessUsers.add(openid);
//				return resultStr;
            } else {
                // 发送失败
                sendFailUsers.add(openid);
            }
        }

        JsonResult jsonResult = new JsonResult();
        if (sendFailUsers.isEmpty()) {
            jsonResult.setCode(ResultCode.SUCCESS);
            jsonResult.setMessage("全部USER通知发送成功");
        }else {
            jsonResult.setCode(ResultCode.FAIL);
            jsonResult.setMessage("USER微信通知发送失败");
        }
        HashMap<String, Object> sendMessageResult = new HashMap<String, Object>();
        sendMessageResult.put("sendSuccessUsers", sendSuccessUsers);
        sendMessageResult.put("sendFailUsers", sendFailUsers);
        jsonResult.setData(sendMessageResult);
        return jsonResult;
    }

    private String generatePushTemplate(JSONObject data, String template_id,String url) {

        JSONObject firstObj = data.getJSONObject("first");
        String firstContent = firstObj.getString("value");
        String firstColor = firstObj.getString("color");

        JSONObject keyword_1_Obj = data.getJSONObject("keyword1");
        String keyword_1_Content = keyword_1_Obj.getString("value");
        String keyword_1_Color = keyword_1_Obj.getString("color");

        JSONObject keyword_2_Obj = data.getJSONObject("keyword2");
        String keyword_2_Content = keyword_2_Obj.getString("value");
        String keyword_2_Color = keyword_2_Obj.getString("color");


//		JSONObject remarkObj = data.getJSONObject("remark");
//		String remarkContent = remarkObj.getString("value");
//		String remarkColor = remarkObj.getString("color");

        StringBuffer buffer = new StringBuffer();
        buffer.append(String.format("\"template_id\":\"%s\"", template_id)).append(",");
        buffer.append(String.format("\"url\":\"%s\"", url)).append(",");
        buffer.append("\"data\":{");
        buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"},", "first", firstContent, firstColor));
        buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"},", "keyword1", keyword_1_Content,
                keyword_1_Color));
        buffer.append(String.format("\"%s\": {\"value\":\"%s\",\"color\":\"%s\"}", "keyword2", keyword_2_Content, keyword_2_Color));

        buffer.append("}");
        buffer.append("}");
        String params = "";
        try {
            params = new String(buffer.toString().getBytes("UTF-8"));
            System.out.println("utf-8 编码：" + params);
            return params;

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    private String getAccessToken() {

        CloseableHttpClient httpCilent = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(WebChatUtil.ACCESS_TOKEN_URL);
        try {
            HttpResponse httpResponse = httpCilent.execute(httpGet);
            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                String entity = EntityUtils.toString(httpResponse.getEntity());
                AccessToken accessToken = JSONObject.parseObject(entity, AccessToken.class);
                return accessToken.getAccess_token();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                httpCilent.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }
}
